/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package idea.de.explorer;



import idea.model.qnrlibrary.CustomResponseDeView;
import java.awt.BorderLayout;
import java.util.List;
import org.netbeans.api.settings.ConvertAsProperties;
import org.netbeans.swing.outline.Outline;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.explorer.view.OutlineView;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;


/**
 * Top component which displays something.
 */
@ConvertAsProperties(
dtd = "-//idea.de.explorer//IDataElems//EN" ,
autostore = false )
@TopComponent.Description(
preferredID = "IDataElemsTopComponent" ,
//iconBase="SET/PATH/TO/ICON/HERE", 
persistenceType = TopComponent.PERSISTENCE_ALWAYS )
@TopComponent.Registration( position = 6, mode = "editor" , openAtStartup = false )
@ActionID( category = "Window" , id = "idea.de.explorer.IDataElemsTopComponent" )
@ActionReferences({
        @ActionReference( path = "Menu/Window" , position = 733  ),
        @ActionReference( path = "Menu/File/Open" , position = 1600, separatorAfter = 1610 )
})
@TopComponent.OpenActionRegistration(
displayName = "#CTL_IDataElemsAction" ,
preferredID = "IDataElemsTopComponent" )
@Messages(
{
    "CTL_IDataElemsAction=Data Elements" ,
    "CTL_IDataElemsTopComponent=Data Elements Window" ,
    "HINT_IDataElemsTopComponent=This is a Data Elements window"
} )
public final class IDataElemsTopComponent extends TopComponent implements ExplorerManager.Provider
{

    private static ExplorerManager exMgr = new ExplorerManager();


    public IDataElemsTopComponent()
    {
        initComponents();
        setName( Bundle.CTL_IDataElemsTopComponent() );
        setToolTipText( Bundle.HINT_IDataElemsTopComponent() );
        setLayout( new BorderLayout() );
        putClientProperty("print.printable", Boolean.TRUE);
        putClientProperty("print.name", "Ed's Data Elements");

        associateLookup( ExplorerUtils.createLookup( exMgr , getActionMap() ) );
    }


    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        lblDE = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        beanTreeView1 = new org.openide.explorer.view.BeanTreeView();

        lblDE.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(lblDE, org.openide.util.NbBundle.getMessage(IDataElemsTopComponent.class, "IDataElemsTopComponent.lblDE.text")); // NOI18N

        jScrollPane1.setViewportView(beanTreeView1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblDE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 371, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(19, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblDE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.openide.explorer.view.BeanTreeView beanTreeView1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblDE;
    // End of variables declaration//GEN-END:variables


    @Override
    public void componentOpened()
    {
        // TODO add custom code on component opening
    }


    @Override
    public void componentClosed()
    {
        // TODO add custom code on component closing
    }


    void writeProperties( java.util.Properties p )
    {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty( "version" , "1.0" );
        // TODO store your settings
    }


    void readProperties( java.util.Properties p )
    {
        String version = p.getProperty( "version" );
        // TODO read your settings according to their version
    }


    @Override
    public ExplorerManager getExplorerManager()
    {
        return exMgr;
    }

    /**
     * Display the list of Data Elements for the given Custom Content Item
     * 
     * @param resultList -- the list of Data Elements
     * @param seq -- for the label
     * @param itemNum -- for the label
     */
    public void displayDataElements(List<CustomResponseDeView> resultList, String seq, String itemNum) {
        ExplorerManager exm = getExplorerManager();
        Children kids = Children.create( new IDataElemsChildFactory(resultList), true);
        Node rootNode = new IDataElemsRootNode(kids);
        
        OutlineView ov = new OutlineView("Sequence");
        
        ov.setPropertyColumns( 
                //"occurrenceSequence", "Sequence",
                "deName", "Data Element",
                "paperAgName", "Paper Attribute Group",
                "electronicAgName", "Electronic Attribute Group",
                "keycode", "Keycode",
                "responseLabel", "Response Label");
        
        Outline o = ov.getOutline();
        o.setRootVisible( false);
        
        o.getColumnModel().getColumn( 0).setMaxWidth(30);
        o.getColumnModel().getColumn( 0).setResizable( false);
        
        lblDE.setText( org.openide.util.NbBundle.getMessage(IDataElemsTopComponent.class, "IDataElemsTopComponent.lblDE.text") 
                + " (for Custom Content Item " + seq + ": \"" + itemNum + "\")");
        add(lblDE, BorderLayout.NORTH);
        add(ov, BorderLayout.CENTER);
        exm.setRootContext( rootNode );
        
    }
}
